package com.leetcode.dynamic;

import java.util.Arrays;

/**
 * @program: leetcode
 * @author: jzhou
 * @date: 2022-11-22 21:09
 * @version: 1.0
 * @description: 最大子数组和   53 题
 **/
public class MaxSubArray {
    public int maxSubArray(int[] nums) {
        int len = nums.length;
        int[] dp = new int[len];
        dp[0] = nums[0];
        for (int i = 1; i < len; i++) {
            dp[i] = nums[i] + dp[i - 1];
            if (dp[i] < nums[i]) dp[i] = nums[i];
        }

        int max = Integer.MIN_VALUE;
        for (int i : dp) {
            if (max < i) max = i;
        }

        return max;

    }

    public int maxSubArray1(int[] nums) {
        int len = nums.length;
        int max = nums[0],tmp = nums[0];
        for (int i = 1; i < len; i++) {
            tmp = nums[i] + tmp;
            if (tmp < nums[i]) tmp = nums[i];
            if (tmp > max) max = tmp;
        }

        return max;

    }
}
